package mya.mes.mdc.logic.dispatch.core;

import mya.common.jpa.context.NoLoginLogic;
import mya.common.jpa.context.ServiceContext;
import mya.common.util.Utils;
import mya.mes.mdc.logic.dispatch.build.DispatchContentBuilder;
import mya.mes.mdc.logic.dispatch.executor.DispatchExecutor;
import mya.mes.mdc.model.alarm.MdcAlarm;
import mya.mes.mdc.model.dispatch.MdcDispatchContent;
import mya.mes.mdc.queries.alarm.MdcAlarmQuery;

import java.util.ArrayList;
import java.util.List;

/**
 * 定期检查报警，是否已经超出设置的时间范围，如果超出，则发送报警
 */
class AlarmTimerTask implements Runnable {

    @Override
    public void run() {
        NoLoginLogic logic = new NoLoginLogic("0") {
            @Override
            protected Object process(ServiceContext context) throws Exception {
                long current = System.currentTimeMillis();
                List<MdcAlarm> list = getOrQuery(context, current, false);
                if (Utils.isNullOrEmpty(list)) {
                    return null;
                }
                List<MdcAlarm> toPerson = filterToPerson(list, current);
                List<MdcAlarm> toManager = filterToManager(list, current);
                if (Utils.isNullOrEmpty(toPerson) && Utils.isNullOrEmpty(toManager)) {
                    return null;
                }
                DispatchContentBuilder dispatchBuilder = new DispatchContentBuilder();
                MdcDispatchContent content =
                        dispatchBuilder.dispatchStartAlarm(context, toPerson, toManager);
                updateFlag(toPerson, toManager);
                return content;
            }
        };

        try {
            MdcDispatchContent content = (MdcDispatchContent) logic.callLogic();
            DispatchExecutor.getInstance().queueDispatch(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void updateFlag(List<MdcAlarm> toPerson, List<MdcAlarm> toManager) {
        if (toPerson != null) {
            for (MdcAlarm alarm : toPerson) {
                alarm.setHasSentToPerson(Boolean.TRUE);
            }
        }
        if (toManager != null) {
            for (MdcAlarm alarm : toManager) {
                alarm.setHasSentToManager(Boolean.TRUE);
            }
        }
    }

    private List<MdcAlarm> getOrQuery(ServiceContext context, long current,
                                      final boolean fromDb) throws Exception {
        List<MdcAlarm> list;
        if (fromDb) {
            list = MdcAlarmQuery.loadNeedDispatch(context, current);
        } else {
            list = AlarmDispatchManager.getInstance().getAlarmCurrent().cloneAlarmList();
        }
        return list;
    }

    private List<MdcAlarm> filterToPerson(List<MdcAlarm> list, long current) {
        List<MdcAlarm> toPerson = null;
        for (MdcAlarm mdcAlarm : list) {
            if (mdcAlarm.getTimeWaitToPerson() == null) {
                continue;
            }
            if (mdcAlarm.hasSentToPerson != null && mdcAlarm.hasSentToPerson) {
                //已经发送
                continue;
            }
            if (mdcAlarm.getTimeWaitToPerson() < current) {
                if (toPerson == null) {
                    toPerson = new ArrayList<>();
                }
                toPerson.add(mdcAlarm);
            }
        }

        return toPerson;
    }


    private List<MdcAlarm> filterToManager(List<MdcAlarm> list, long current) {
        List<MdcAlarm> toManager = null;
        for (MdcAlarm mdcAlarm : list) {
            if (mdcAlarm.getTimeWaitToManager() == null) {
                continue;
            }
            if (mdcAlarm.hasSentToManager != null && mdcAlarm.hasSentToManager) {
                //已经发送
                continue;
            }
            if (mdcAlarm.getTimeWaitToManager() < current) {
                if (toManager == null) {
                    toManager = new ArrayList<>();
                }
                toManager.add(mdcAlarm);
            }
        }

        return toManager;
    }

}
